package com.luxoft.scoremanager.web;

import com.luxoft.scoremanager.server.model.web.Component;
import com.luxoft.scoremanager.server.model.web.ScorePackage;
import com.luxoft.scoremanager.server.model.web.StaticData;
import com.luxoft.scoremanager.server.service.PackageManagerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/scoremanager")
public class PackageManagerController {
    @Autowired
    private PackageManagerService packageManagerService;

    @Autowired
    private static Logger log = Logger.getLogger(PackageManagerController.class);

    public void setPackageManagerService(PackageManagerService packageManagerService) {
        this.packageManagerService = packageManagerService;
    }

    @RequestMapping(value = "/packageId/", method = RequestMethod.GET)
    public String handle1Request() {
        log.info("Drawing new data" );
        return "index";
    }

    @RequestMapping(value = "/packageId/{id}", method = RequestMethod.GET)
    public String handle1Request1(@PathVariable("id") Integer id) {
        log.info("Get package with ID = " + id );
        return "index";
    }

    @RequestMapping(value = "/packageId/{id}/package/read", method = RequestMethod.GET)
    public @ResponseBody ScorePackage readPackage(@PathVariable("id") Integer id) {
        log.info("Package: " + id + "  reading package data from JSON");
        return packageManagerService.getScorePackageById(id);
    }

    @RequestMapping(value = "/packageId/{id}/staticdata/read", method = RequestMethod.GET)
    public @ResponseBody List<StaticData> readStaticData(@PathVariable("id") Integer id) {
        log.info("Package: " + id + "  reading StaticData from JSON");
        return packageManagerService.getAllStaticData();
    }

    @RequestMapping(value = "/packageId/{id}/components/read", method = RequestMethod.GET)
    public @ResponseBody List<Component> readComponents(@PathVariable("id") Integer id) {
        log.info("Package: " + id + "  reading Components from JSON");
        return packageManagerService.getAllComponentByIdOfScorePackage(id);
    }

    @RequestMapping(value = "/packageId/{id}/components/update", method = RequestMethod.POST)
    public @ResponseBody Component updateComponent(@PathVariable("id") Integer id, @RequestBody Component component) {
        log.info("Package: " + id + ";  updating component: " + component);
        return component;
    }

    @RequestMapping(value = "/packageId/{id}/package/update", method = RequestMethod.POST)
    public @ResponseBody ScorePackage updatePackage(@PathVariable("id") Integer id, @RequestBody ScorePackage scorePackage) {
        log.info("Package: " + id + ";  updating package: " + scorePackage);
        packageManagerService.updateScorePackage(scorePackage);
        return scorePackage;
    }

    @RequestMapping(value = "/packageId/{id}/fullpackage/read", method = RequestMethod.GET)
    public @ResponseBody ScorePackage readFullPackage(@PathVariable("id") Integer id) {
        ScorePackage scorePackage;
        scorePackage = packageManagerService.getScorePackageById(id);
        return scorePackage;
    }

    @RequestMapping(value = "/packageId/{id}/fullpackage/update", method = RequestMethod.POST)
    public @ResponseBody ScorePackage updateFullPackage(@PathVariable("id") Integer id, @RequestBody ScorePackage scorePackage) {
        log.info(scorePackage);
        packageManagerService.updateScorePackage(scorePackage);
        return scorePackage;
    }

}
